package com.guoqing.test.controller;

import com.guoqing.test.entity.User;
import com.guoqing.test.entity.UserB;
import com.guoqing.test.example.ClassTwo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;

@RestController
public class AController {

    @RequestMapping("/hello")
    public String Hello(String message) {
        return message;
    }


    /**
     * 金额相减
     *
     * @param numOne
     * @param numTwo
     * @return
     */
    public static String subtract(String numOne, String numTwo) {
        if (StringUtils.isBlank(numOne) || StringUtils.isBlank(numTwo)) {
            return "金额格式有误";
        }
        BigDecimal numOneBig = new BigDecimal(numOne);
        BigDecimal numTwoBig = new BigDecimal(numTwo);
        //取绝对值并保留两位小数
        BigDecimal total = numOneBig.subtract(numTwoBig).setScale(2, BigDecimal.ROUND_HALF_UP);
        return total.toString();
    }

    /**
     * 金额累加
     *
     * @param numOne
     * @param numTwo
     * @return
     */
    public static String add(String numOne, String numTwo) {
        if (StringUtils.isBlank(numOne) || StringUtils.isBlank(numTwo)) {
            return "0";
        }
        BigDecimal numOneBig = new BigDecimal(numOne);
        BigDecimal numTwoBig = new BigDecimal(numTwo);
        //保留两位小数
        BigDecimal total = numOneBig.add(numTwoBig).setScale(2, BigDecimal.ROUND_HALF_UP);
        return total.toString();
    }

    public static void main(String[] args) {
        String subtract = subtract("7", "5");
        System.out.println(subtract);
        System.out.println(add("5", subtract));

        User user = new User();
        user.setMakeName("aaaa");
        user.setModifyName("bbbb");
//        System.out.println(user);
//        user.setModifyData(user);
//        System.out.println(user);
        user.setGender("male,female");
        System.out.println(user);
        UserB b = new UserB();
        BeanUtils.copyProperties(user, b);
        System.out.println(b);
        UserB convert = convert(user, UserB.class);
        System.out.println(convert);
    }

    public static <T> T convert(Object source, Class<T> clazz) {
        if (source == null || clazz == null) {
            return null;
        }
        try {
            T target = clazz.newInstance();
            BeanUtils.copyProperties(source, target);
            return target;
        } catch (InstantiationException | IllegalAccessException e) {
            throw new RuntimeException("对象转换异常", e);
        }
    }

    @RequestMapping("/haha")
    public ClassTwo haha() {
        ClassTwo classTwo = new ClassTwo("1", "2", "3");
        return classTwo;
    }
}
